package com.jxd.ordering.controller;

import com.jxd.ordering.annotation.CheckPermission;
import com.jxd.ordering.exception.CommonEnum;
import com.jxd.ordering.exception.serviceexception.PermissionNotAllowedException;
import com.jxd.ordering.model.Dept;
import com.jxd.ordering.model.ResultBody;
import com.jxd.ordering.service.IDeptService;
import com.jxd.ordering.service.IUserService;
import com.jxd.ordering.util.PermissionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @ClassName DeptController
 * @Description TODO
 * @Author Ankailong
 * @Date 2022/9/27
 * @Version 1.0
 */
@RestController
public class DeptController {
    @Autowired
    private IDeptService deptService;
    @Autowired
    private IUserService userService;

    /**
     * 新增部门信息
     * level: 3
     * @param dept 部门实体类
     * @return 是否新增成功
     */
    @CheckPermission("3")
    @RequestMapping("/addDept")
    public ResultBody addDept(@RequestBody Dept dept) {
        return deptService.addDept(dept) ? ResultBody.success() : ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * 修改部门信息
     * level: 3
     * @param dept 部门实体类
     * @return 是否修改成功
     */
    @CheckPermission("3")
    @RequestMapping("/editDept")
    public ResultBody editDept(@RequestBody Dept dept) {
        return deptService.editDept(dept) ? ResultBody.success() : ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * 批量删除部门信息
     * level: 3
     * @param deptnos 部门编号列表
     * @return 是否删除成功
     */
    @CheckPermission("3")
    @RequestMapping("/delDept")
    public ResultBody delDept(@RequestBody List<Integer> deptnos) {
        return deptService.delDept(deptnos.stream().mapToInt(Integer::intValue).toArray()) ? ResultBody.success() : ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * 分页查询全部部门信息
     * level: 3
     * @param queryMap 包含int page, int limit, String dname
     * @return 查询结果
     */
    @CheckPermission("3")
    @RequestMapping("/getDeptByPage")
    public Map<String, Object> getDeptByPage(@RequestBody Map<String,String> queryMap) {
        return deptService.getDeptByPage(queryMap);
    }

    /**
     * 按部门编号查询部门信息
     * level: 3
     * @param deptno 部门编号
     * @return 部门实体类
     */
    @RequestMapping("/getDeptByDeptno")
    public Dept getDeptByDeptno(int deptno, HttpServletRequest request) throws PermissionNotAllowedException {
        //临时授权
        if (deptno != userService.getDeptnoByUserid(Integer.parseInt(request.getAttribute("userid").toString()))) {
            PermissionUtil.PermissionControl(request.getAttribute("permission"), 3);
        }
        return deptService.getDeptByDeptno(deptno);
    }

    /**
     * 查询全部部门信息
     * level: 3
     * @return 查询结果
     */
    @CheckPermission("3")
    @RequestMapping("/getAllDept")
    public List<Dept> getAllDept() {
        return deptService.getAllDept();
    }

    /**
     * 查询部门数
     * level: 3
     * @return 查询结果
     */
    @CheckPermission("3")
    @RequestMapping("getDeptNum")
    public int selectDeptNum() {
        return deptService.getDeptNum();
    }
}
